/*
 * Copyright 2012 Niklas Rehfeld.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.madpie.ui;

import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import org.madpie.schemas.DiskType;
import org.madpie.schemas.PartitionType;

/**
 *
 * @author Niklas Rehfeld
 */
public class DiskListRenderer extends JTextArea implements ListCellRenderer
{

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
                                                  int index, boolean isSelected,
                                                  boolean cellHasFocus)
    {
        if (isSelected)
        {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        }
        else
        {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        if (!(value instanceof DiskType))
        {
            Logger.getLogger(DiskListRenderer.class.getName()).
                    log(Level.WARNING,
                    "{0} is not a DiskType.\n using basic string renderer.",
                    value.getClass().getName());
            return RenderOther(list, value, index, isSelected, cellHasFocus);
        }
        DiskType disk = (DiskType) value;


        StringBuilder out = new StringBuilder(disk.getPath());
        out.append("\t size: ").append(disk.getSize() / 1024L / 1024L);
        out.append("MB\n");
        out.append("Partitions:\n");
        for (PartitionType p : disk.getPartition())
        {
            out.append("\t").append(p.getName()).append(" ");
            out.append(p.getSize() / 1024 / 1024).append("MB");
            out.append("(").append(p.getType()).append(")");
            if (p.isBootable())
            {
                out.append(" **Bootable**");
            }
            out.append("\n");
        }
        this.setText(out.toString());
        return this;
    }

    private Component RenderOther(JList list, Object value,
                                  int index, boolean isSelected,
                                  boolean cellHasFocus)
    {
        this.setText(value.toString());
        return this;
    }
}
